<template>


  <a-card :bordered="false">
    <div class="table-page-search-wrapper">

    </div>

    <div class="table-operator">
      <a-row>
        <a-col :span="14">
          <a-button type="primary" icon="plus" @click="$router.push('/policyManage/policyAdd')">新建</a-button>
        </a-col>
        <a-col :span="10" class="text-align-right">
          <a-input-search
            placeholder="请输入关键字"
            @search="onSearch"
            enterButton="查询"
            size="large" />
        </a-col>
      </a-row>


    </div>

    <a-table
      ref="table"
      size="default"
      :columns="columns"
      :dataSource="tableData"
      :showAlertInfo="true"
      @onSelect="onChange"
    >
      <a slot="infoAction"  @click="handleDetail(record)" slot-scope="text,record,index">
        {{text}}
      </a>

      <span slot="number" slot-scope="text,record,index">
                {{index + 1}}
            </span>
      <span slot="action" slot-scope="text, record">

              <a @click="handleEdit(record)"><a-icon type="edit" />编辑</a>
                <a-divider type="vertical"/>
              <a  style="color: red;">
                   <a-popconfirm title="确定删除吗？" @confirm="handleDelete(record)" okText="确定" cancelText="取消">
                           <a-icon type="delete" />删除
                    </a-popconfirm>
              </a>

            </span>
    </a-table>

    <a-modal
      title="编辑"
      width="50%"
      v-model="visible"
      @ok="saveFormData"
    >
      <policyFormTemplate :policyFormDataJson = "policyFormData" @fatherEditMethod="getlandResourceList" ref="landForm"></policyFormTemplate>
    </a-modal>

    <a-modal
      title="详情"
      width="40%"
      v-model="detailBool"
      @ok="detailBool=false"
    >
      <policyInfo :policyFormDataJson = "policyFormData" ></policyInfo>
    </a-modal>

  </a-card>

</template>

<script>
  import STable from '@/components/table/'
  import ATextarea from "ant-design-vue/es/input/TextArea"
  import AInput from "ant-design-vue/es/input/Input"
  import moment from "moment"
  import {getRoleList, getServiceList} from '@/api/manage'
  import {getBase64} from '../../utils/util'
  import indexedDB from '../../db/db'
  import {landResource} from "../../db/landResource"
  import policyFormTemplate from './policy-form-template'
  import policyInfo from './policy-info'
  export default {
    name: "landResourceList",
    components: {
      AInput,
      ATextarea,
      STable,
      policyFormTemplate,
      policyInfo
    },
    data() {
      return {
        detailBool:false,
        visible: false,
        searchText:"",
        labelCol: {
          xs: {span: 24},
          sm: {span: 5},
        },
        wrapperCol: {
          xs: {span: 24},
          sm: {span: 12},
        },
        policyFormData:{
          title:"",
          sendPeople:"",
          date:"",
          area:"",
          keyWord:"",
          content:"",
        },
        mdl: {},

        // 高级搜索 展开/关闭
        advanced: true,
        // 查询参数
        queryParam: {},
        // 表头
        columns: [
          {
            title: '序号',
            dataIndex: 'number',
            scopedSlots: {customRender: 'number'},
          },
          {
            title: '标题',
            dataIndex: 'title',
            scopedSlots: {customRender: 'infoAction'},
          },
          {
            title: '日期',
            dataIndex: 'date',
          },
          {
            title: '发布人',
            dataIndex: 'sendPeople',
          },

          {
            title: '操作',
            dataIndex: 'action',
            width: '200px',
            scopedSlots: {customRender: 'action'},
          },
        ],
        // 加载数据方法 必须为 Promise 对象
        loadData: parameter => {
          return getServiceList(Object.assign(parameter, this.queryParam))
            .then(res => {
              return res.result
            })
        },

        selectedRowKeys: [],
        selectedRows: [],
        //数据库
        tableData: [],
      }
    },
    created() {
      getRoleList({t: new Date()})
    },
    mounted() {
      indexedDB.initDB()
      // indexedDB.batInit('landResource', landResource);// 初始化数据
      this.getlandResourceList()
    },
    methods: {
      getlandResourceList() {
        let that = this
        this.visible = false;
        this.searchText = "";
        indexedDB.handleDataByCursor('policy').then(res => {

          if (res.length > 0) {
            that.tableData = res
            console.log(that.tableData)
          }else{
            that.tableData = [];
          }
        })
      },
      onSearch(e){
        console.log(e)
        this.searchText = e;
        // this.getlandResourceList()
        let that = this
        indexedDB.handleDataByIndex('policy',this.searchText, "title").then(res => {

          if (res.length > 0) {
            that.tableData = res
            console.log(that.tableData)
          }else{
            that.tableData = []
          }
        })
      },
      handleEdit(record) {
        this.visible = true
        this.$nextTick(()=>{
          this.$refs.landForm.handleEdit(record);
        })
      },
      handleVerify(record){
        this.policyFormData = Object.assign({}, record)
        this.verifyBool = true;
      },
      handleDetail(record){
        this.policyFormData = Object.assign({}, record)
        this.detailBool = true;
      },
      handleDelete(record) {
        indexedDB.delete({name:"policy"},record.id).then((res)=>{
          if(res==true){
            this.$message.info("删除成功！");
            this.getlandResourceList()
          }else{
            this.$message.error("删除失败！");
          }
        })
      },
      onChange(row) {
        this.selectedRowKeys = row.selectedRowKeys
        this.selectedRows = row.selectedRows

        console.log(this.$refs.table)
      },
      toggleAdvanced() {
        this.advanced = !this.advanced
      },

      resetSearchForm() {
        this.queryParam = {
          date: moment(new Date()),
        }
      },

      saveFormData(){
        this.$refs.landForm.editeSaveFormData();

      },



    },
    watch: {
      /*
      'selectedRows': function (selectedRows) {
        this.needTotalList = this.needTotalList.map(item => {
          return {
            ...item,
            total: selectedRows.reduce( (sum, val) => {
              return sum + val[item.dataIndex]
            }, 0)
          }
        })
      }
      */
    },
  }
</script>